package com.ruoyi.xysl.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.system.domain.vo.TreeSelect;
import com.xysl.api.domain.XyslMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xysl.mapper.SysXyslMenuMapper;
import com.ruoyi.xysl.service.ISysXyslMenuService;

/**
 * 菜单Service业务层处理
 *
 * @author xysl
 * @date 2025-01-11
 */
@Service
public class SysXyslMenuServiceImpl implements ISysXyslMenuService
{
    @Autowired
    private SysXyslMenuMapper sysXyslMenuMapper;

    /**
     * 查询菜单
     *
     * @param id 菜单主键
     * @return 菜单
     */
    @Override
    public XyslMenu selectXyslMenuById(Long id)
    {
        return sysXyslMenuMapper.selectXyslMenuById(id);
    }

    /**
     * 查询菜单列表
     *
     * @param xyslMenu 菜单
     * @return 菜单
     */
    @Override
    public List<XyslMenu> selectXyslMenuList(XyslMenu xyslMenu)
    {
        return sysXyslMenuMapper.selectXyslMenuList(xyslMenu);
    }

    @Override
    public List<Long> selectMenuListByRoleId(Long roleId) {
        return sysXyslMenuMapper.selectMenuListByRoleId(roleId,true);
    }

    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<XyslMenu> menus) {
        List<XyslMenu> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }


    @Override
    public List<XyslMenu> buildMenuTree(List<XyslMenu> menus) {
        List<XyslMenu> returnList = new ArrayList<XyslMenu>();
        List<Long> tempList = menus.stream().map(XyslMenu::getId).collect(Collectors.toList());
        for (Iterator<XyslMenu> iterator = menus.iterator(); iterator.hasNext();)
        {
            XyslMenu menu = (XyslMenu) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId()))
            {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = menus;
        }
        return returnList;
    }



    /**
     * 递归列表
     *
     * @param list 分类表
     * @param t 子节点
     */
    private void recursionFn(List<XyslMenu> list, XyslMenu t)
    {
        // 得到子节点列表
        List<XyslMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (XyslMenu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<XyslMenu> getChildList(List<XyslMenu> list, XyslMenu t)
    {
        List<XyslMenu> tlist = new ArrayList<XyslMenu>();
        Iterator<XyslMenu> it = list.iterator();
        while (it.hasNext())
        {
            XyslMenu n = (XyslMenu) it.next();
            if (n.getParentId().longValue() == t.getId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<XyslMenu> list, XyslMenu t)
    {
        return getChildList(list, t).size() > 0;
    }

    /**
     * 新增菜单
     *
     * @param xyslMenu 菜单
     * @return 结果
     */
    @Override
    public int insertXyslMenu(XyslMenu xyslMenu)
    {
        return sysXyslMenuMapper.insertXyslMenu(xyslMenu);
    }

    /**
     * 修改菜单
     *
     * @param xyslMenu 菜单
     * @return 结果
     */
    @Override
    public int updateXyslMenu(XyslMenu xyslMenu)
    {
        return sysXyslMenuMapper.updateXyslMenu(xyslMenu);

    }

    /**
     * 批量删除菜单
     *
     * @param ids 需要删除的菜单主键
     * @return 结果
     */
    @Override
    public int deleteXyslMenuByIds(Long[] ids)
    {
        return sysXyslMenuMapper.deleteXyslMenuByIds(ids);
    }

    /**
     * 删除菜单信息
     *
     * @param id 菜单主键
     * @return 结果
     */
    @Override
    public int deleteXyslMenuById(Long id)
    {
        return sysXyslMenuMapper.deleteXyslMenuById(id);
    }
}
